﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Chess
{
    public partial class ChessMain: Form
    {
        public static Game game;
        public static string filename = "";
        PictureBox[,] picSquares;
        Square squareFrom = null;
        Moves moves = new Moves();
       
        public ChessMain()
        {
            InitializeComponent();
        }
        [STAThread]
        static void Main()
        {
            Application.Run(new ChessMain());
        }
      
        public Image GetPieceImage(Piece piece)
        {
            return imagePiece.Images[piece.ImageIndex];
        }

        public void picSquareClick(object sender,EventArgs e)
        {
            Square squareClick;
            Piece piece;
            int ordinal = Convert.ToInt32(((PictureBox)sender).Tag);
            if (squareFrom == null)
            {
                squareClick = Board.GetSquare(ordinal);
                piece = squareClick.Piece;
                if (piece != null && piece.Player.isColor == Game.isPlayer.isColor)
                {
                    squareFrom = squareClick;
                    moves = new Moves();
                    piece.ValidMove(moves);
                }
                else
                {
                    squareFrom = null;
                    moves = new Moves();
                }
            }
            else
            {
                squareClick = Board.GetSquare(ordinal);
                piece = squareClick.Piece;
                if (piece == null || piece != null && piece.Player.isColor != Game.isPlayer.isColor)
                {
                    foreach (Move move in moves)
                    {
                        if (move.To == squareClick)
                        {
                            squareFrom = null;
                            moves = new Moves();
                            Game.MakeMove(move.Piece, move.To);
                            RenderBoard();
                            break;
                        }
                    }
                    squareFrom = null;
                    moves = new Moves();
                }
                else
                    if (piece != null && piece == squareFrom.Piece)
                    {
                        squareFrom = null;
                        moves = new Moves();
                    }
                    else
                        if (piece != null)
                        {
                            moves = new Moves();
                            squareFrom = squareClick;
                            squareFrom.Piece.ValidMove(moves);
                        }
                        else
                        {
                            squareFrom = null;
                            moves = new Moves();
                        }
            }

            RenderBoard();
        }        
        public void RenderBoard()
        {           
            Square square;
            for (int ordinal = 0; ordinal < 128; ordinal++)
            {
                square = Board.GetSquare(ordinal);

                if (square != null)
                {
                    if (square.isColor == Square.Color.WHITE)                 
                        picSquares[square.File,square.Rank].BackColor = Color.Wheat;                   
                    else                  
                        picSquares[square.File,square.Rank].BackColor = Color.Peru;                   

                    if (square.Piece == null)                   
                        picSquares[square.File,square.Rank].Image = null;                   
                    else                   
                        picSquares[square.File,square.Rank].Image =GetPieceImage(square.Piece);            
                }
            }
            if (squareFrom != null)
            {
                picSquares[squareFrom.File,squareFrom.Rank].BackColor = Color.AliceBlue;
                foreach (Move move in moves)
                {
                    picSquares[move.To.File, move.To.Rank].BackColor = Color.AliceBlue;
                }
            }

        }
       
        public void CreateBoard()
        {
            PictureBox picSquare;
            Square square;           
            picSquares = new PictureBox[8,8];           
            for (int ordinal = 0; ordinal < 128; ordinal++)
            {
                square = Board.GetSquare(ordinal);
                if (square != null)
                {
                    picSquare = new System.Windows.Forms.PictureBox();
                    picSquare.Left = square.File * 49 + 8;
                    picSquare.Top = 49 * 7 - square.Rank * 49 + 8;
                    if (square.isColor == Square.Color.WHITE)
                        picSquare.BackColor = Color.Wheat;
                    else
                        picSquare.BackColor = Color.Peru;
                    
                    picSquare.Size = new System.Drawing.Size(49,49);
                    picSquare.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
                    picSquare.TabIndex = 0;
                    picSquare.TabStop = false;
                    picSquare.Tag = square.Ordinal;
                    picSquare.Click += new System.EventHandler(this.picSquareClick);
                    pnlBoard.Controls.Add(picSquare);
                    picSquares[square.File,square.Rank] = picSquare;
                }
            }           
        }       
        public void NewGame()
        {
            Game.New();
            RenderBoard();
            this.Text = Game.FileName + " - " + Application.ProductName;
        }

        public void OpenGame()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Chess files (*.Chess)|*.Chess";
            openFileDialog.FilterIndex = 2;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
                if (openFileDialog.FileName != "")
                {
                    Game.Open(openFileDialog.FileName);
                    RenderBoard();
                }
            this.Text = Game.FileName + " - " + Application.ProductName;
        }

        public void SaveGame()
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Chess files (*.Chess)|*.Chess";
            saveFileDialog.FilterIndex = 2;
            saveFileDialog.FileName = Game.FileName;          
            if (saveFileDialog.ShowDialog() == DialogResult.OK)                
                if (saveFileDialog.FileName != "")
                    Game.Save(saveFileDialog.FileName);

            this.Text = Game.FileName + " - " + Application.ProductName;
          
        }

        public void UndoAllMoves()
        {
            Game.UndoAllMoves();
            RenderBoard();
        }

        public void UndoMove()
        {
            Game.UndoMove();
            RenderBoard();
        }  

        public void RedoMove()
        {
            Game.RedoMove();
            RenderBoard();
        }

        public void RedoAllMoves()
        {
            Game.RedoAllMoves();
            RenderBoard();
        }		

        public void Exit()
        {
            Application.Exit();
        }
        
        public void toolbarClick(object sender,ToolBarButtonClickEventArgs e)
        {
            switch (e.Button.Tag.ToString())
            {
                case "New":
                    NewGame();
                    break;

                case "Open":
                    OpenGame();
                    break;

                case "Save":
                    SaveGame();
                    break;

                case "UndoAllMoves":
                    UndoAllMoves();
                    break;

                case "UndoMove":
                    UndoMove();
                    break;                
               
                case "RedoMove":
                    RedoMove();
                    break;

                case "RedoAllMoves":
                    RedoAllMoves();
                    break;

                case "Exit":
                    Exit();
                    break;

            }
        }
        public void LoadMain(object sender,EventArgs e)
        {
            game = new Game();
            CreateBoard();
            RenderBoard();
            this.Text = Game.FileName + " - " + Application.ProductName;                
        }
    }
}
